#include "FileEncodeOper.h"
#include "commonuse.h"
#include <QFileDialog>
#include <QLineEdit>
#include <QPushButton>
#include <QStandardPaths>
#include <QHBoxLayout>
#include <QTextStream>

const QString kDevelop = "[DEVELOPER]";
const QString location = QStandardPaths::writableLocation(
                             QStandardPaths::DesktopLocation);
FileEnCodeWidget::FileEnCodeWidget(QWidget *par): QWidget(par)
{
    QHBoxLayout *hDeLayOut = new QHBoxLayout;
    QLineEdit *pEdit = new QLineEdit("select file");
    pEdit->setReadOnly(true);
    hDeLayOut->addWidget(pEdit, 8);
    QPushButton *pSel = new QPushButton("SelectFile", this);
    QObject::connect(pSel, &QPushButton::clicked, this, [ &, pEdit]() {
        QString filter = "encode file(*.enc *.sec);;All files(*.*)";
        _deFilePath.clear();
        _deFilePath = QFileDialog::getOpenFileName(this, "Select file", location,
                                                   filter);
        if (_deFilePath.isEmpty()) return;
        pEdit->setText(_deFilePath);
    });
    QPushButton *pDecode = new QPushButton("Decode", this);
    QObject::connect(pDecode, &QPushButton::clicked, this, [ &, pEdit]() {
        QString deFile = _deFilePath.left(_deFilePath.lastIndexOf("."));
        bool ok = FileOperation::decryptFile(_deFilePath, deFile);
        ok &= removeLinesWithKeyword(deFile, kDevelop);
        if (ok) pEdit->setText("Decode finish!");
    });
    hDeLayOut->addWidget(pSel, 1);
    hDeLayOut->addWidget(pDecode, 1);
    hDeLayOut->addStretch();

    QHBoxLayout *pEnLayout = new QHBoxLayout;
    QLineEdit *pEnEdit = new QLineEdit("select file");
    pEnEdit->setReadOnly(true);
    pEnLayout->addWidget(pEnEdit, 8);
    QPushButton *pEnSel = new QPushButton("SelectFile");
    QObject::connect(pEnSel, &QPushButton::clicked, this, [ &, pEnEdit]() {
        QString filter = "Plaint file(*.txt *.log);;XML(*.xml *.cfg);;All files(*.*)";
        _enFilePath.clear();
        _enFilePath = QFileDialog::getOpenFileName(this, "Select file", location,
                                                   filter);
        if (_enFilePath.isEmpty()) return;
        pEnEdit->setText(_enFilePath);
    });
    QPushButton *pEncode = new QPushButton("Encode", this);
    QObject::connect(pEncode, &QPushButton::clicked, this, [ &, pEnEdit]() {
        QString enFile = _enFilePath + ".sec";
        bool ok = FileOperation::encryptFile(_enFilePath, enFile);
        ok &= removeLinesWithKeyword(enFile, kDevelop);
        if (ok) pEnEdit->setText("Encode finish!");
    });
    pEnLayout->addWidget(pEnSel, 1);
    pEnLayout->addWidget(pEncode, 1);
    pEnLayout->addStretch();
    QVBoxLayout *mainLay = new QVBoxLayout(this);
    mainLay->addLayout(hDeLayOut);
    mainLay->addLayout(pEnLayout);
    mainLay->addStretch();
}

bool FileEnCodeWidget::removeLinesWithKeyword(const QString &filename,
                                              const QString &keyWord)
{
    QFile file(filename);
    if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
        return false;
    QTextStream in(&file);
    QTextStream out(&file);
    QStringList outputLines;
    while (!in.atEnd()) {
        QString line = in.readLine();
        if (!line.contains(keyWord))
            outputLines.append(line);
    }
    file.resize(0);
    for (const QString &line : outputLines)
        out << line << '\n';
    return true;
}

FileEnCodeWidget::~FileEnCodeWidget()
{

}